﻿#include "SelectAllButtonBar.h"
#include "ui_SelectAllButtonBar.h"

namespace BaseWidget
{
    SelectAllButtonBar::SelectAllButtonBar(QWidget* parent)
        : QWidget(parent)
        , ui(new Ui::SelectAllButtonBar())
    {
        ui->setupUi(this);
        //this->setGeometry(0, 0, 100, 100);
        ui->checkBox_chooseAllCheckBox->setTristate(true);
        // 全选
         connect(ui->checkBox_chooseAllCheckBox, &QCheckBox::stateChanged, this,
             [this](int state)
             {
                 if (state == Qt::CheckState::Checked)
                 {
                     emit signal_ModifyListSelectedStatus(2);
                 }
                 else if (state == Qt::CheckState::PartiallyChecked)
                 {
                     ui->checkBox_chooseAllCheckBox->setChecked(true);
                     emit signal_ModifyListSelectedStatus(1);
                 }
                 else if (state == Qt::CheckState::Unchecked)
                 {
                     emit signal_ModifyListSelectedStatus(0);
                 }
             }
        );
    }

    SelectAllButtonBar::~SelectAllButtonBar()
    {
        delete ui;
    }

    void SelectAllButtonBar::setCheckBoxState(int state, bool isListItemChanged)
    {
        if (isListItemChanged)
        {
            ui->checkBox_chooseAllCheckBox->blockSignals(true);
        }
        switch (state)
        {
        case 0:
            ui->checkBox_chooseAllCheckBox->setCheckState(Qt::Unchecked);
            break;
        case 1:
            ui->checkBox_chooseAllCheckBox->setCheckState(Qt::PartiallyChecked);
            break;
        case 2:
            ui->checkBox_chooseAllCheckBox->setCheckState(Qt::Checked);
            break;
        default:
            break;
        }
        ui->checkBox_chooseAllCheckBox->repaint();
        if (isListItemChanged)
        {
            ui->checkBox_chooseAllCheckBox->blockSignals(false);
        }
    }


}
